package com.acme.javaschool.intense.sems.sem3logging;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileAppender extends Appender {

    private final BufferedWriter outStream;
    File file;

    public FileAppender(int bufferSize, String fileName) throws IOException {
        super(bufferSize, AppenderType.FILE);
        this.file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        outStream = new BufferedWriter(new FileWriter(file));
    }

    @Override
    protected void write(Object... message) throws IOException {
        if (message != null) {
            for (Object part : message) {
                outStream.write(part.toString());
            }
            outStream.write("\n");
            outStream.flush();
        }
    }
}
